package com.bourne.config;

import com.bourne.component.InterConfig;
import com.bourne.constant.NumConstant;
import com.bourne.constant.StringConstant;
import com.bourne.util.BeanRegisterUtils;
import com.bourne.util.ClassUtil;
import com.bourne.util.SpringBeanTools;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@Slf4j
public class WebAppConfigurer implements WebMvcConfigurer {

    @Autowired
    private BeanRegisterUtils beanRegisterUtils;

    @Value("${interceptor.path:com.bourne.interceptor}")
    private String interceptorPath;

    @Autowired
    private InterConfig interConfig;

    @SneakyThrows
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        Map<String, String> interMap = new HashMap<>();
        List<String> list = ClassUtil.getClazzName(interceptorPath, false);
        for (String inter : list) {
            String[] split = StringUtils.split(inter, StringConstant.POINT);
            String name = ClassUtil.lowercase(split[split.length - NumConstant.ONE]);
            interMap.put(name, inter);
        }
        log.info("加载的拦截器...");
        if (CollectionUtils.isNotEmpty(interConfig.getData())) {
            List<Map<String, String>> data = interConfig.getData();
            for (Map<String, String> dataMap : data) {
                String interName = null;
                Object auth = null;
                Class<?> aClass = null;
                InterceptorRegistration interceptorRegistration = null;
                for (String key : dataMap.keySet()) {
                    String value = dataMap.get(key);
                    if (StringUtils.isBlank(value)) {
                        continue;
                    }
                    if (StringUtils.equals(StringConstant.INTER_KEY_NAME, key)) {
                        interName = value;
                        String path = interMap.get(value);
                        aClass = Class.forName(path);
                        auth = aClass.newInstance();
                        beanRegisterUtils.registerBean(value, auth.getClass());
                        HandlerInterceptor bean = (HandlerInterceptor) SpringBeanTools.getBean(value);
                        interceptorRegistration = registry.addInterceptor(bean);
                    }
                    if (StringUtils.equals(StringConstant.INTER_KEY_CONTAIN, key)) {
                        assert interceptorRegistration != null;
                        String[] contains = StringUtils.split(value, StringConstant.COMMA);
                        for (String contain : contains) {
                            interceptorRegistration.addPathPatterns(contain);
                        }
                    }

                    if (StringUtils.equals(StringConstant.INTER_KEY_EXCLUDE, key)) {
                        assert interceptorRegistration != null;
                        String[] contains = StringUtils.split(value, StringConstant.COMMA);
                        for (String contain : contains) {
                            interceptorRegistration.excludePathPatterns(contain);
                        }

                    }
                }
                log.info("add {} success", interName);
            }
        }
        log.info("...加载的拦截器");
    }
}
